package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

     @Autowired
     private ClazzService clazzService;
     @Override
    public PageResult<Student> pageList(StudentQueryParam student) {

          //1.创建一个page对象方便下面分页
        Page<Student>page=Page.of(student.getPage(),student.getPageSize());
         page.addOrder(OrderItem.desc("update_time"));
      //条件分页
    page= lambdaQuery().like(student.getName()!=null&& !student.getName().isEmpty(), Student::getName, student.getName())
                .eq(student.getDegree()!=null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId()!=null, Student::getClazzId, student.getClazzId())
                .page(page);

        List<Student> records = page.getRecords();
        //将records中的clazz_id与clazz表中的clazz_name对应起来
        for (Student record : records) {
            Integer clazzId = record.getClazzId();
            //判断clazzId是否为空，且clazzid是否有对应的clazz对象
            if(clazzId!=null&&clazzService.getById(clazzId)!=null){
                record.setClazzName(clazzService.getById(clazzId).getName());
            }
        }

        return new PageResult<>(page.getTotal(),records);
    }
}
